package com.appstore.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class ConnectionManager {

	private static ConnectionManager connectionManager;
	private DataSource dataSource;

	private ConnectionManager() throws NamingException {
		Context ctx = new InitialContext();
		this.dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/mysql");
	}

	public Connection getConnetion() throws SQLException {
		return this.dataSource.getConnection();
	}

	public static ConnectionManager createInstance() throws NamingException {
		if (connectionManager == null) {
			connectionManager = new ConnectionManager();
		}
		return connectionManager;
	}

	public static ConnectionManager getConnectionManager() {
		return connectionManager;
	}
}
